function LinkedList() {
    let Node = function (element) {
        this.element = element
        this.next = null
    };
    
    let length = 0;
    let head = null;

    // 向链表尾部追加元素
    this.append = function (element) {
        let node = new Node(element),current
        if(head === null){
            // 列表中第一个节点
            head = node
        }else{
            current = head
            //列表循环，直到找到最后一项
            while(current.next){
                current = current.next
            }
            //找到最后一项，将其next赋为node,建立连接
            current.next = node
        }
        length++;  //更新列表长度
    };

    // 在任意位置插入元素
    this.insert = function (position,element) {
        // 检查越界值
        if(position >= 0 && position <= length){
            let node = new Node(element),
                currrent = head,
                previous,
                index = 0;
            if(position === 0){  // 在第一个位置添加
                node.next =currrent;
                head = node;
            }else{
                while(index++ < position){
                    previous = currrent;
                    current = current.next;
                }
                node.next = currrent;
                previous.next = node
            }
            length++; //更新列表的长度
            return true
        }else{
            return false
        }
    };

    // 从链表中移除元素
    this.removeAt = function (position) {
        //检查越界值
        if(position > -1 && position < length){
            let current = head,previous,index = 0
            // 移除第一项
            if (position === 0){
                head = current.next;
            }else{
                while(index++ < position){
                    previous = current;
                    current = current.next;
                }
                // 将previous与current的下一项链接起来;跳过current,从而移除他
                previous.next = current.next;
            }
            length--;
            return current.element;
        }else{
            return null;
        }
    };
    
    this.remove = function (element) {
        let index = this.indexOf(element)
        return this.removeAt(index)
    };
    
    this.indexOf = function (element) {
        let current = head,
            index = -1;
        while (current){
            if(element == current.element){
                return index;
            }
            index++;
            current = current.next;
        }
        return -1;
    };
    
    this.isEmpty = function () {
        return length === 0
    };
    
    this.size = function () {
        return length;
    };
    
    this.getHead = function () {
        return head;
    };
    
    this.toString = function () {
        let current = head,
        string = '';
        while(current){
            string += current.element + (current.next ? 'n':'')
            current = current.next
        }
        return string ;
    };
    
    this.print = function () {

    }
    
}

let list = new LinkedList();
list.append(13)
list.append(15)
console.log(list.size())


// 双向链表
function DoubleLinkedList() {
    let Node = function(element){
        this.element = element;
        this.next = null;
        this.pre = null;
    };
    let length = 0;
    let head = null;
    let tail = null;
    // 在任意位置插入元素
    this.insert = function () {
        // 检查越界值
        if(position >= 0 && position <= length){
            let node = new Node(element)
            //    ....
        }
    }
}